import 'package:flutter/material.dart';
import 'package:dio/dio.dart';

import 'light_belt.dart';

class Door extends StatefulWidget {
  @override
  _State createState() => _State();
}

class _State extends State<Door> {
  @override
  Widget build(BuildContext context) {
    return Wrap(children: <Widget>[
      Stack(
        alignment: AlignmentDirectional.center,
        children: <Widget>[
          Container(
            alignment: Alignment.topLeft,
            child: Image.asset('images/灯带_0001.png',
                width: MediaQuery.of(context).size.width / 1.3,
                height: MediaQuery.of(context).size.height / 8),
          ),
          Container(
            alignment: Alignment(-0.5, 0.0),
            child: Text('公司大门',
                style: TextStyle(color: Colors.white, fontSize: 18)),
          ),
        ],
      ),
      Container(
          width: MediaQuery.of(context).size.width,
          height: (MediaQuery.of(context).size.height / 9.45) * 7,
          child: GridView.count(
            shrinkWrap: true,
            crossAxisCount: 2,
            mainAxisSpacing: 10,
            crossAxisSpacing: 10,
            childAspectRatio: 1.8,
            children: [OffOrOn(), OffOrOn()],
          )),
    ]);
  }

  OffOrOn() {
    return Stack(
      overflow: Overflow.visible,
      children: <Widget>[
        Container(
          child: Image.asset('images/电视_0029.png'),
        ),
        Positioned(
          left: mediaQuery.size.width / 8,
          top: mediaQuery.size.height / 25,
          child: RaisedButton(
            child: Text("开门"),
            color: Colors.teal,
            onPressed: () {
              DoorFunction().then((value) => showDialog(
                  context: context,
                  builder: (context) {
                    return AlertDialog(
                      title: Text("提示"),
                      content: Text(value),
                      actions: <Widget>[
                        FlatButton(
                          child: Text("确认"),
                          onPressed: () {
                            Navigator.of(context).pop(true); //关闭对话框
                          },
                        )
                      ],
                    );
                  }));
            },
          ),
        ),
      ],
    );
  }

  Future<String> DoorFunction() async {
    Response response = await Dio()
        .get("http://192.168.10.122:8090/renren-fast/search/controlDoor");
    if (response.statusCode == 200) {
      return "正在开门请稍等";
    }
    return "打开门失败";
  }
}
